﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace GBSHouse.UptimeRobot {
	public class UptimeRobotMonitor {

		/// <summary>
		/// Gets or sets the ID.
		/// </summary>
		/// <value>
		/// The ID.
		/// </value>
		[JsonProperty("id")]
		public int ID { get; set; }

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>
		/// The name.
		/// </value>
		[JsonProperty("friendlyname")]
		public string Name { get; set; }

		/// <summary>
		/// Gets or sets the value.
		/// </summary>
		/// <value>
		/// The value.
		/// </value>
		[JsonProperty("url")]
		public string Value { get; set; }

		/// <summary>
		/// Gets or sets the type.
		/// </summary>
		/// <value>
		/// The type.
		/// </value>
		[JsonProperty("type")]
		public int Type { get; set; }

		/// <summary>
		/// Gets or sets the subtype.
		/// </summary>
		/// <value>
		/// The subtype.
		/// </value>
		[JsonProperty("subtype")]
		public string SubType { get; set; }

		/// <summary>
		/// Gets or sets the type of the keyword.
		/// </summary>
		/// <value>
		/// The type of the keyword.
		/// </value>
		[JsonProperty("keywordtype")]
		public int KeywordType { get; set; }

		/// <summary>
		/// Gets or sets the keyword.
		/// </summary>
		/// <value>
		/// The keyword.
		/// </value>
		[JsonProperty("keywordvalue")]
		public string Keyword { get; set; }

		/// <summary>
		/// Gets or sets the HTTP username.
		/// </summary>
		/// <value>
		/// The HTTP username.
		/// </value>
		[JsonProperty("httpusername")]
		public string HttpUsername { get; set; }

		/// <summary>
		/// Gets or sets the HTTP password.
		/// </summary>
		/// <value>
		/// The HTTP password.
		/// </value>
		[JsonProperty("httppassword")]
		public string HttpPassword { get; set; }

		/// <summary>
		/// Gets or sets the port.
		/// </summary>
		/// <value>
		/// The port.
		/// </value>
		[JsonProperty("port")]
		public string Port { get; set; }

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>
		/// The status.
		/// </value>
		[JsonProperty("status")]
		public int Status { get; set; }

		/// <summary>
		/// Gets or sets the total uptime.
		/// </summary>
		/// <value>
		/// The total uptime.
		/// </value>
		[JsonProperty("alltimeuptimeratio")]
		public double? TotalUptime { get; set; }

		/// <summary>
		/// Gets or sets the custom uptime.
		/// </summary>
		/// <value>
		/// The custom uptime.
		/// </value>
		[JsonProperty("customuptimeratio")]
		public double? CustomUptime { get; set; }

		/// <summary>
		/// Gets or sets the type of the uptime robot monitor type.
		/// </summary>
		/// <value>
		/// The type of the uptime robot monitor type.
		/// </value>
		[JsonIgnore]
		public UptimeRobotMonitorType UptimeRobotMonitorType {
			get { return (UptimeRobotMonitorType)this.Type; }
			set { this.Type = (int)value; }
		}

		/// <summary>
		/// Gets or sets the type of the uptime robot monitor sub type.
		/// </summary>
		/// <value>
		/// The type of the uptime robot monitor sub type.
		/// </value>
		[JsonIgnore]
		public UptimeRobotMonitorSubType UptimeRobotMonitorSubType {
			get {
				int subtype = 0;
				if(!String.IsNullOrEmpty(this.SubType) && Int32.TryParse(this.SubType, out subtype))
					return (UptimeRobotMonitorSubType)subtype;
				return UptimeRobotMonitorSubType.Unknown;
			}
			set {
				if (value == UptimeRobot.UptimeRobotMonitorSubType.Unknown)
					this.SubType = String.Empty;
				else
					this.SubType = ((int)value).ToString(); 
			}
		}

		/// <summary>
		/// Gets or sets the uptime robot monitor status.
		/// </summary>
		/// <value>
		/// The uptime robot monitor status.
		/// </value>
		[JsonIgnore]
		public UptimeRobotMonitorStatus UptimeRobotMonitorStatus {
			get { return (UptimeRobotMonitorStatus)this.Status; }
			set { this.Status = (int)value; }
		}

		/// <summary>
		/// Gets or sets the type of the uptime robot monitor keyword.
		/// </summary>
		/// <value>
		/// The type of the uptime robot monitor keyword.
		/// </value>
		[JsonIgnore]
		public UptimeRobotMonitorKeywordType UptimeRobotMonitorKeywordType {
			get { return (UptimeRobotMonitorKeywordType)this.KeywordType; }
			set { this.KeywordType = (int)value; }
		}

		/// <summary>
		/// Gets or sets the logs.
		/// </summary>
		/// <value>
		/// The logs.
		/// </value>
		[JsonProperty("log")]
		public List<UptimeRobotMonitorLog> Logs { get; set; }

		/// <summary>
		/// Gets or sets the contacts.
		/// </summary>
		/// <value>
		/// The contacts.
		/// </value>
		[JsonProperty("monitoralertcontact")]
		public List<UptimeRobotContact> Contacts { get; set; }
		
		public UptimeRobotMonitor() {
			this.Logs = new List<UptimeRobotMonitorLog>();
			this.Contacts = new List<UptimeRobotContact>();
		}
	}

	public enum UptimeRobotMonitorType : int {
		Unknown = 0,
		HTTP = 1,
		Keyword = 2,
		Ping = 3,
		Port = 4,
	}

	public enum UptimeRobotMonitorSubType : int {
		Unknown = 0,
		HTTP = 1,
		HTTPS = 2,
		FTP = 3,
		SMTP = 4,
		POP3 = 5,
		IMAP = 6,
		Custom = 99
	}

	public enum UptimeRobotMonitorStatus : int {
		Paused = 0,
		NotCheckedYet = 1,
		Up = 2,
		SeemsDown = 3,
		Down = 4
	}

	public enum UptimeRobotMonitorKeywordType : int {
		NotExists = 0,
		Exists = 1
	}
}
